using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Game;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

namespace IQIGame.Onigao.GamePlay
{
    public class UIN_LevelMapFloorPanel : UIBaseNode
    {
        #region Template Generate,don't modify

        protected class UIB_UIN_LevelMapFloorPanel
        {
            #region ObjectBinding Generate

            public GameObject floorItem { protected set; get; }
            public ExToggleGroup toggleGroup { protected set; get; }

            public virtual void InitBinding(ObjectBinding __binding)
            {
                __binding.TryGetVariableValue<GameObject>("floorItem", out var __tbv0);
                floorItem = __tbv0;
                __binding.TryGetVariableValue<ExToggleGroup>("toggleGroup", out var __tbv1);
                toggleGroup = __tbv1;
            }

            #endregion ObjectBinding Generate
        }

        #endregion Template Generate,don't modify


        #region fields

        private List<ExToggle> _toggles = new();

        private int _floorCnt;

        private int _curFloor;

        #endregion fields

        #region properties

        protected UIB_UIN_LevelMapFloorPanel ui { get; set; }
        protected UIMsgDispatcher uiMsgDispatcher => _msgDispatcher as UIMsgDispatcher;

        #endregion properties

        protected override void BeforeInit()
        {
            ui = new UIB_UIN_LevelMapFloorPanel();
            ui.InitBinding(csObjBind);
        }

        protected override void OnInit()
        {
            AddFloor(ui.floorItem, 0);
            _toggles.First().isOn = true;
        }

        protected override void OnDispose()
        {
            _toggles.Clear();
            _toggles = null;
        }

        protected override void OnShow()
        {
            _floorCnt = 5;
            Refresh();
        }

        protected override void OnHide()
        {

        }

        #region methonds

        private void Refresh()
        {
            for (var i = 0; i < _floorCnt; i++)
            {
                if (i == _toggles.Count)
                {
                    AddFloor(Object.Instantiate(ui.floorItem), i);
                }

                _toggles[i].SetActive(true);
            }

            for (var i = _floorCnt; i < _toggles.Count; i++)
            {
                _toggles[i].SetActive(false);
            }
        }

        private void OnToggleValueChanged(int index, bool isOn)
        {
            if (!isOn) return;
            _curFloor = index;
            Debug.Log($"CurFloor:{_curFloor}");
        }

        private void AddFloor(GameObject go, int index)
        {
            var toggle = go.GetComponent<ExToggle>();
            toggle.onValueChanged.AddListener((isOn) => OnToggleValueChanged(index, isOn));
            for (var i = 0; i < go.transform.childCount; i++)
            {
                var child = go.transform.GetChild(i);
                if (child.name.Equals("Label"))
                {
                    child.GetComponent<ExText>().text = $"Floor{index}";
                    break;
                }
            }

            ui.toggleGroup.RegisterToggle(toggle);
            _toggles.Add(toggle);
        }

        #endregion methonds
    }
}